package study.javabasic.communication.waitnotify;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangshuhan
 * @date 2019/3/21
 */
public class WaitNotifyTest {

    public static void main(String[] args) {
        Object o = new Object();
        List<String> list = new ArrayList<>();


        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o) {
                    System.out.println("开始等待====");
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("do something");
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o) {
                    for (int i = 0; i < 10; i++) {
                        list.add(String.valueOf(i));
                        System.out.println("==添加第"+i+"个元素");
                        if (list.size() == 5) {
                            o.notifyAll();
                            System.out.println("========通知了老铁");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
        }).start();


    }
}
